package com.lyz.dz.des.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lyz.dz.des.pojo.classinfo;
import com.lyz.dz.des.pojo.lookforEntiy;
import com.lyz.dz.des.pojo.message;
import com.lyz.dz.des.pojo.status;
import com.lyz.dz.des.service.classInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.stream.FileImageOutputStream;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.io.IOException;

@RestController
@ResponseBody
@Validated
 @RequestMapping("/user/classinfo")
public class classInfoController {
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    classInfoService classInfoService;

    message msg;
    @PostMapping("/add")
    public String addClassINfo(@Validated classinfo classinfo) throws JsonProcessingException {
        msg = new message();
        boolean save = classInfoService.save(classinfo);
        if (save){
            msg.setCode(status.ServerCode_success);
            msg.setMessage(status.operation_success);

        }else {
            msg.setCode(status.ServerCode_success);
            msg.setMessage(status.operation_success);
        }

        return objectMapper.writeValueAsString(msg);
    }

    @PostMapping("/delete")
    public String delClassInfo(@NotBlank String uid) throws JsonProcessingException {
            msg = new  message();
        boolean b = classInfoService.removeById(uid);
        if (b){
            msg.setCode(status.ServerCode_success);
            msg.setMessage(status.operation_success);

        }else {
            msg.setCode(status.ServerCode_success);
            msg.setMessage(status.operation_success);
        }
        return objectMapper.writeValueAsString(msg);
    }
    @PostMapping("/update")
    public String updateClassInfo(@Validated classinfo classinfo) throws JsonProcessingException{
        msg = new message();
        boolean b = classInfoService.updateById(classinfo);
        if (b){
            msg.setCode(status.ServerCode_success);
            msg.setMessage(status.operation_success);

        }else {
            msg.setCode(status.ServerCode_success);
            msg.setMessage(status.operation_success);
        }
        return objectMapper.writeValueAsString(msg);
    }
    @PostMapping("/select")
    public String selectClassInfo(Page page ,@Validated lookforEntiy lookforEntiy){
        msg = new message();
        QueryWrapper<classinfo> classinfoQueryWrapper = new QueryWrapper<>();
        classinfoQueryWrapper.like(lookforEntiy.getTeacherName()!=null,"teacher_name",lookforEntiy.getTeacherName())
                              .eq(lookforEntiy.getTeacherUid() !=null,"teacher_uid",lookforEntiy.getTeacherUid())
                .like(lookforEntiy.getClassName()!= null,"class_name",lookforEntiy.getClassName())
                .between("class_date", lookforEntiy.getStart(), lookforEntiy.getEnd())
                .orderByAsc("class_date")
                .orderByAsc("class_name")
                .orderByAsc("class_what_day");
        Page page1 = classInfoService.page(page, classinfoQueryWrapper);

//        if ()

        return null;
    }
    // 文件的真实路径
    @Value("${file.uploadFolder}")
    String realBasePath;
    @Value("${file.accessPath}")
    String accessPath;
    @PostMapping("/upload")
    public String uploadFile(MultipartFile multipartFile) throws IOException {

        File ss = new File("ss");
        String absolutePath = ss.getAbsolutePath();
        File file = new File(realBasePath + "2.png");
        FileImageOutputStream fileImageOutputStream = new FileImageOutputStream(file);
        fileImageOutputStream.write(multipartFile.getBytes());
        fileImageOutputStream.flush();
        fileImageOutputStream.close();
        return "succ";
    }
}
